/*
 * @Author: 陈德基
 * @Date: 2021-11-15 16:37:06
 * @LastEditTime: 2021-11-18 16:20:54
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\page\main\subpage\dynamic.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'package:copy_gsy_github_app_flutter/common/model/Event.dart';
import 'package:copy_gsy_github_app_flutter/page/main/index.dart';
import 'package:copy_gsy_github_app_flutter/page/main/widgets/event_widget.dart';
import 'package:copy_gsy_github_app_flutter/utils/event_utils.dart';
import 'package:copy_gsy_github_app_flutter/widget/empty_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class DynamicWidget extends GetView<MainController> {
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  void _onRefresh() async {
    await controller.refreshDynamic();
    _refreshController.refreshCompleted();
  }

  void _onLoading() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Obx(() => SmartRefresher(
              controller: _refreshController,
              enablePullDown: true,
              enablePullUp: true,
              onRefresh: _onRefresh,
              onLoading: _onLoading,
              child: controller.state.dynamics.length > 0
                  ? ListView.builder(
                      physics: AlwaysScrollableScrollPhysics(),
                      itemCount: controller.state.dynamics.length,
                      itemBuilder: (c, i) {
                        return _renderEventItem(controller.state.dynamics[i]);
                      })
                  : Container(
                      child: Center(
                        child: EmptyView(),
                      ),
                    ),
            )),
      ),
    );
  }

  _renderEventItem(Event e) {
    var model = EventViewModel.fromEventMap(e);
    return EventItem(
      model,
      onPressed: () => EventUtils.ActionUtils(e, ''),
    );
  }
}
